package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.service.impl.CategoryServiceImpl;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import io.swagger.models.properties.PropertyBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/brand")
public class BrandController {

    private static final Integer superPid = 0;

    @Autowired
    private AdminService adminService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @RequestMapping("/list")
    public Object list(Integer page,Integer limit,String id,String name,String sort,String order){
        IPage<DtsBrand> iPage = brandService.findByPage(page, limit, id, name, sort, order);

        Map<String,Object> data = new HashMap<>();

        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return ResponseUtil.ok(data);
    }


    @RequestMapping("/catAndAdmin")
    public Object catAndAdmin(){
        List<DtsAdmin> admins = adminService.findAll();

        List<Map<String,Object>> adminList = new ArrayList<>();
        if (admins != null){
            for (DtsAdmin admin : admins) {
                Map<String,Object> adminMap = new HashMap<>();
                adminMap.put("value",admin.getId());
                adminMap.put("label", admin.getUsername());
                adminList.add(adminMap);
            }
        }

        List<DtsCategory> categories = categoryService.selectByPid();
        List<CatVo> catVoList = new ArrayList<>();
        if (categories != null){
            for (DtsCategory category : categories) {
                List<Map<String,Object>> childs = new ArrayList<>();
                CatVo catVo = new CatVo();
                catVo.setValue(category.getId());
                catVo.setLabel(category.getName());
                List<DtsCategory> dtsCategories = categoryService.selectById(category);
                if (dtsCategories != null){
                    for (DtsCategory dtsCategory : dtsCategories) {
                        Map<String,Object> child = new HashMap<>();
                        child.put("value", dtsCategory.getId());
                        child.put("label", dtsCategory.getName());
                        childs.add(child);
                    }
                }
                catVo.setChildren(childs);
                catVoList.add(catVo);
            }
        }
        Map<String,Object> data = new HashMap<>();
        data.put("adminList", adminList);
        data.put("categoryList",catVoList);

        return ResponseUtil.ok(data);
    }

    @RequestMapping("/create")
    public Object create(@RequestBody DtsCategory category){
        int add = categoryService.add(category);
        if (add == 0){
            return ResponseUtil.badArgument();
        }
        return ResponseUtil.ok(category);
    }

    @RequestMapping("/update")
    public Object update(@RequestBody  DtsCategory category){
        int update = categoryService.update(category);
        if (update == 0){
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(category);
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsCategory category){
        categoryService.deleteById(category);
        return ResponseUtil.ok();
    }

}
